
#include "PrimaryIDManager.h"

#include <ALittleServer/MysqlSystem/StatementQuery.h>
#include <ALittleServer/MysqlSystem/StatementResult.h>

#include <boost/thread/lock_guard.hpp>

namespace ALittle
{

PrimaryIDManager::PrimaryIDManager()
:m_id(0)
{

}

PrimaryIDManager::~PrimaryIDManager()
{

}

void PrimaryIDManager::Init( const std::string& db_name, const std::string& table_name )
{
	StatementQuery query;
	std::string sql;
	sql.append("SELECT id FROM ").append(table_name).append(" ORDER BY id desc LIMIT 1");
	query.SetSQL(db_name.c_str(), sql.c_str());
	query.Execute();

	StatementResult& result = query.GetResult();
	result >> m_id;
	result.Fetch();
}

unsigned long long PrimaryIDManager::CreateID()
{
	boost::lock_guard<boost::mutex> locker(m_mutex);
	return ++m_id;
}

unsigned long long PrimaryIDManager::NextID()
{
	boost::lock_guard<boost::mutex> locker(m_mutex);
	return m_id + 1;
}

} // ALittle